﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration;
using MaxStudio.Concrete.Component;
using System.Collections;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RPTMaterialCost : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }
        #region Private Method


        private void BindGridView()
        {
            RPTMaterialCostService RPTMaterialCostServiceObj = ServiceFactory.getRPTMaterialCostService();
            OperationResult<List<tb_RPT_MaterialCost>> result = RPTMaterialCostServiceObj.GetAllMaterialCost();

            if (result.operationSuccess)
            {
                gvRPTMaterialCostList.DataSource = result.obj;
                gvRPTMaterialCostList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            RPTMaterialCostService RPTMaterialCostServiceObj = ServiceFactory.getRPTMaterialCostService();
            OperationResult<tb_RPT_MaterialCost> resultEdit = RPTMaterialCostServiceObj.GetMaterialCostById(id);

            if (resultEdit.operationSuccess == true)
            {

                //  txtEditInputDate.Text = resultEdit.obj.InputDate;

                txtEditProdCode.Text = resultEdit.obj.ProdCode;
                txtEditSlump.Text = resultEdit.obj.Slump;
                txtEditWaterBinderRate.Text = resultEdit.obj.WaterBinderRate;
                txtEditSandRatio.Text = resultEdit.obj.SandRatio;
                txtEditCement.Text = resultEdit.obj.Cement;
                txtEditWater.Text = resultEdit.obj.Water;
                txtEditFine.Text = resultEdit.obj.Fine;
                txtEditCoarseSand.Text = resultEdit.obj.CoarseSand;
                txtEditGravel.Text = resultEdit.obj.Gravel;
                txtEditAdmixture.Text = resultEdit.obj.Admixture;
                txtEditAsh.Text = resultEdit.obj.Ash;
                txtEditDensity.Text = resultEdit.obj.Density;
                txtEditCost.Text = resultEdit.obj.Cost;
                txtEditCementPrice.Text = resultEdit.obj.CementPrice;
                txtEditWaterPrice.Text = resultEdit.obj.WaterPrice;
                txtEditFinePrice.Text = resultEdit.obj.FinePrice;
                txtEditCoarseSandPrice.Text = resultEdit.obj.CoarseSandPrice;
                txtEditGravelPrice.Text = resultEdit.obj.GravelPrice;
                txtEditAdmixturePrice.Text = resultEdit.obj.AdmixturePrice;
                txtEditAshPrice.Text = resultEdit.obj.AshPrice;

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RPTMaterialCostService RPTMaterialCostServiceObj = ServiceFactory.getRPTMaterialCostService();
            tb_RPT_MaterialCost instance = new tb_RPT_MaterialCost();

            instance.ProdCode = txtProdCode.Text;
            instance.Slump = txtSlump.Text;
            instance.WaterBinderRate = txtWaterBinderRate.Text;
            instance.SandRatio = txtSandRatio.Text;
            instance.Cement = txtCement.Text;
            instance.Water = txtWater.Text;
            instance.Fine = txtFine.Text;
            instance.CoarseSand = txtCoarseSand.Text;
            instance.Gravel = txtGravel.Text;
            instance.Admixture = txtAdmixture.Text;
            instance.Ash = txtAsh.Text;
            instance.Density = txtDensity.Text;
            instance.Cost = txtCost.Text;
            instance.CementPrice = txtCementPrice.Text;
            instance.WaterPrice = txtWaterPrice.Text;
            instance.FinePrice = txtFinePrice.Text;
            instance.CoarseSandPrice = txtCoarseSandPrice.Text;
            instance.GravelPrice = txtGravelPrice.Text;
            instance.AdmixturePrice = txtAdmixturePrice.Text;
            instance.AshPrice = txtAshPrice.Text;


            OperationResult<tb_RPT_MaterialCost> result = RPTMaterialCostServiceObj.CreateMaterialCost(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }
            RPTMaterialCostService RPTMaterialCostServiceObj = ServiceFactory.getRPTMaterialCostService();
            tb_RPT_MaterialCost entity = new tb_RPT_MaterialCost();

            entity.Id = editId;
            entity.ProdCode = txtEditProdCode.Text;
            entity.Slump = txtEditSlump.Text;
            entity.WaterBinderRate = txtEditWaterBinderRate.Text;
            entity.SandRatio = txtEditSandRatio.Text;
            entity.Cement = txtEditCement.Text;
            entity.Water = txtEditWater.Text;
            entity.Fine = txtEditFine.Text;
            entity.CoarseSand = txtEditCoarseSand.Text;
            entity.Gravel = txtEditGravel.Text;
            entity.Admixture = txtEditAdmixture.Text;
            entity.Ash = txtEditAsh.Text;
            entity.Density = txtEditDensity.Text;
            entity.Cost = txtEditCost.Text;
            entity.CementPrice = txtEditCementPrice.Text;
            entity.WaterPrice = txtEditWaterPrice.Text;
            entity.FinePrice = txtEditFinePrice.Text;
            entity.CoarseSandPrice = txtEditCoarseSandPrice.Text;
            entity.GravelPrice = txtEditGravelPrice.Text;
            entity.AdmixturePrice = txtEditAdmixturePrice.Text;
            entity.AshPrice = txtEditAshPrice.Text;

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RPT_MaterialCost> resultEdit = RPTMaterialCostServiceObj.ModifyMaterialCost(entity);
            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RPTMaterialCostService RPTMaterialCostServiceObj = ServiceFactory.getRPTMaterialCostService();
            tb_RPT_MaterialCost deleteEntity = new tb_RPT_MaterialCost();
            deleteEntity.Id = deleteId;
            RPTMaterialCostServiceObj.DeleteMaterialCost(deleteEntity);

            BindGridView();
        }

        protected void gvRPTMaterialCostList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvRPTMaterialCostList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRPTMaterialCostList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
